using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Scene.Video;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
namespace MicroMsg.UI.UserContrl
{
	public class BaseChatContent : Button
	{
		private const string TAG = "BaseChatContent";
		private const string ImgType = "IMG";
		private const string VoiceType = "VOICE";
		private const string TxtType = "TXT";
		private BaseChatElement mChatElement;
		private ChatMsg chatMsg_0;
		private bool mBIsTransparentContainer;
		private bool mIsDeleteEnable = true;
		private MenuItem delItem;
		private MenuItem resendItem;
		private MenuItem addToFav;
		private MenuItem copyItem;
		private MenuItem forwardItem;
		private MenuItem rereceiveItem;
		private ContextMenu menu;
		private bool mIsLoaded;
		private Border Container;
		private Path CornerPath;
		public static DependencyProperty ContentTypeProperty = DependencyProperty.Register("ContentType", typeof(string), typeof(BaseChatContent), new PropertyMetadata("TXT", new PropertyChangedCallback(BaseChatContent.OnItemPropertyChanged)));
		public static DependencyProperty BkBrushProperty = DependencyProperty.Register("BkBrush", typeof(Brush), typeof(BaseChatContent), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(255, 208, 208, 208)), new PropertyChangedCallback(BaseChatContent.OnItemPropertyChanged)));
		public static DependencyProperty CornerImgProperty = DependencyProperty.Register("CornerImg", typeof(ImageSource), typeof(BaseChatContent), new PropertyMetadata(null, new PropertyChangedCallback(BaseChatContent.OnItemPropertyChanged)));
		private static Color RightTxtNormalColor = Color.FromArgb(255, 255, 255, 255);
		private static SolidColorBrush LeftTxtNormalBrush = new SolidColorBrush(Color.FromArgb(255, 150, 207, 88));
		private static SolidColorBrush RightTxtNormalBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
		private static SolidColorBrush LeftVoiceNormalBrush = new SolidColorBrush(Color.FromArgb(255, 150, 207, 88));
		private static SolidColorBrush RightVoiceNormalBrush = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
		private static SolidColorBrush DriftBottleVoiceNormalBrush = new SolidColorBrush(Color.FromArgb(255, 45, 156, 220));
		private Storyboard glittersStoryboard;
		private bool mIsGlitterFlag;
		private bool mShouldGlitter;
		public static GlittersService gGlittersService = new GlittersService();
		private GlittersService mGlittersService = BaseChatContent.gGlittersService;
		private bool _contentLoaded;
		public bool IsDeleteEnable
		{
			get
			{
				return this.mIsDeleteEnable;
			}
			set
			{
				this.mIsDeleteEnable = value;
				if (this.delItem == null)
				{
					return;
				}
				if (this.mIsDeleteEnable)
				{
					this.delItem.set_IsEnabled(true);
					return;
				}
				this.delItem.set_IsEnabled(false);
			}
		}
		public string ContentType
		{
			get
			{
				return (string)base.GetValue(BaseChatContent.ContentTypeProperty);
			}
			set
			{
				base.SetValue(BaseChatContent.ContentTypeProperty, value);
			}
		}
		public Brush BkBrush
		{
			get
			{
				return (Brush)base.GetValue(BaseChatContent.BkBrushProperty);
			}
			set
			{
				base.SetValue(BaseChatContent.BkBrushProperty, value);
			}
		}
		public ImageSource CornerImg
		{
			get
			{
				return (ImageSource)base.GetValue(BaseChatContent.CornerImgProperty);
			}
			set
			{
				base.SetValue(BaseChatContent.CornerImgProperty, value);
			}
		}
		public bool IsGlitterFlag
		{
			get
			{
				return this.mIsGlitterFlag;
			}
			set
			{
				this.mIsGlitterFlag = value;
				this.RefreshGlitterFlag();
			}
		}
		private bool ShouldGlitter
		{
			get
			{
				return this.mShouldGlitter;
			}
			set
			{
				if (value == this.mShouldGlitter)
				{
					return;
				}
				this.mShouldGlitter = value;
				this.DoGlitter();
			}
		}
		public virtual void SetChatElement(BaseChatElement chatElement)
		{
			if (chatElement != null)
			{
				this.mChatElement = chatElement;
			}
		}
		public virtual void SetChatMsg(ChatMsg Msg)
		{
			this.chatMsg_0 = Msg;
			if (this.Container != null)
			{
				this.Container.set_BorderThickness(new Thickness(0.0, 0.0, 0.0, 1.0));
			}
			this.mBIsTransparentContainer = false;
			this.RefreshMenu();
		}
		public void RefreshMenu()
		{
			if (this.menu == null)
			{
				return;
			}
			if (this.delItem == null)
			{
				return;
			}
			if (this.resendItem == null)
			{
				return;
			}
			if (this.rereceiveItem == null)
			{
				return;
			}
			if (this.copyItem == null)
			{
				return;
			}
			if (this.forwardItem == null)
			{
				return;
			}
			this.setMenuStrings();
			if (this.chatMsg_0 == null)
			{
				ContextMenuService.SetContextMenu(this, null);
				return;
			}
			MsgUIStatus status = this.chatMsg_0.GetStatus();
			this.menu.set_Visibility(0);
			if (!this.chatMsg_0.IsTxt() && !this.chatMsg_0.isQmessageTxt())
			{
				if (status == MsgUIStatus.Processing)
				{
					this.copyItem.set_Visibility(1);
					ContextMenuService.SetContextMenu(this, null);
				}
				else
				{
					if (this.chatMsg_0.IsVoice() && status == MsgUIStatus.Fail && this.chatMsg_0.nIsSender != 1)
					{
						this.copyItem.set_Visibility(1);
						this.delItem.set_Visibility(0);
						this.resendItem.set_Visibility(1);
						this.rereceiveItem.set_Visibility(0);
						ContextMenuService.SetContextMenu(this, this.menu);
					}
					else
					{
						if (status != MsgUIStatus.Success)
						{
							if (this.chatMsg_0.nIsSender == 1)
							{
								if (status == MsgUIStatus.Fail)
								{
									this.copyItem.set_Visibility(1);
									this.delItem.set_Visibility(0);
									this.resendItem.set_Visibility(0);
									this.rereceiveItem.set_Visibility(1);
									ContextMenuService.SetContextMenu(this, this.menu);
									goto IL_272;
								}
								goto IL_272;
							}
						}
						this.copyItem.set_Visibility(1);
						this.delItem.set_Visibility(0);
						this.resendItem.set_Visibility(1);
						this.rereceiveItem.set_Visibility(1);
						ContextMenuService.SetContextMenu(this, this.menu);
					}
				}
			}
			else
			{
				if (status == MsgUIStatus.Processing)
				{
					this.copyItem.set_Visibility(0);
					this.delItem.set_Visibility(1);
					this.resendItem.set_Visibility(1);
					this.rereceiveItem.set_Visibility(1);
					ContextMenuService.SetContextMenu(this, this.menu);
				}
				else
				{
					if (status != MsgUIStatus.Success)
					{
						if (this.chatMsg_0.nIsSender == 1)
						{
							if (status == MsgUIStatus.Fail)
							{
								this.copyItem.set_Visibility(0);
								this.delItem.set_Visibility(0);
								this.resendItem.set_Visibility(0);
								this.rereceiveItem.set_Visibility(1);
								ContextMenuService.SetContextMenu(this, this.menu);
								goto IL_272;
							}
							goto IL_272;
						}
					}
					this.copyItem.set_Visibility(0);
					this.delItem.set_Visibility(0);
					this.resendItem.set_Visibility(1);
					this.rereceiveItem.set_Visibility(1);
					ContextMenuService.SetContextMenu(this, this.menu);
				}
			}
			IL_272:
			if (this.chatMsg_0.IsTxt() || this.chatMsg_0.isQmessageTxt() || this.chatMsg_0.IsImage() || this.chatMsg_0.IsVideo() || this.chatMsg_0.IsCustomSmiley())
			{
				if (status != MsgUIStatus.Success)
				{
					if (this.chatMsg_0.nIsSender == 1)
					{
						goto IL_2C7;
					}
				}
				this.forwardItem.set_Visibility(0);
				goto IL_2E1;
			}
			IL_2C7:
			this.forwardItem.set_Visibility(1);
			IL_2E1:
			if (this.isShowAddFavMenu())
			{
				this.addToFav.set_Visibility(0);
				return;
			}
			this.addToFav.set_Visibility(1);
		}
		public void CreatMenu()
		{
			this.menu = new ContextMenu();
			this.menu.set_Width(200.0);
			this.menu.set_Background(R.theme.CONTEXTMENU_BACKGROUND_BRUSH);
			this.menu.IsZoomEnabled = false;
			this.copyItem = new MenuItem();
			this.copyItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.copyItem.Click += new RoutedEventHandler(this.copyItem_Click);
			this.menu.get_Items().Add(this.copyItem);
			this.delItem = new MenuItem();
			this.delItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.delItem.Click += new RoutedEventHandler(this.delItem_Click);
			this.menu.get_Items().Add(this.delItem);
			this.forwardItem = new MenuItem();
			this.forwardItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.forwardItem.Click += new RoutedEventHandler(this.forwardItem_Click);
			this.menu.get_Items().Add(this.forwardItem);
			this.resendItem = new MenuItem();
			this.resendItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.resendItem.Click += new RoutedEventHandler(this.resendItem_Click);
			this.menu.get_Items().Add(this.resendItem);
			this.rereceiveItem = new MenuItem();
			this.rereceiveItem.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.rereceiveItem.Click += new RoutedEventHandler(this.rereceiveItem_Click);
			this.menu.get_Items().Add(this.rereceiveItem);
			this.addToFav = new MenuItem();
			this.addToFav.set_Foreground(R.theme.CONTEXTMENU_FOREGROUND_BRUSH);
			this.addToFav.Click += new RoutedEventHandler(this.addToFav_Click);
			this.menu.get_Items().Add(this.addToFav);
			this.setMenuStrings();
			ContextMenuService.SetContextMenu(this, this.menu);
			this.menu.set_Visibility(1);
		}
		private void setMenuStrings()
		{
			if (this.copyItem != null)
			{
				this.copyItem.Header = strings.ChatItemCopy;
			}
			if (this.delItem != null)
			{
				this.delItem.Header = strings.ChatItemDelete;
			}
			if (this.forwardItem != null)
			{
				this.forwardItem.Header = strings.ChatItem_Forward;
			}
			if (this.resendItem != null)
			{
				this.resendItem.Header = strings.ChatItemRetry;
			}
			if (this.rereceiveItem != null)
			{
				this.rereceiveItem.Header = strings.ChatItemReReceive;
			}
			if (this.addToFav != null)
			{
				this.addToFav.Header = strings.ChatEmojiAddToFav;
			}
		}
		private bool isShowAddFavMenu()
		{
			if ((this.chatMsg_0.IsTxt() || this.chatMsg_0.isQmessageTxt()) && EmojiManager.GetInstance().IsEmojiMsg(this.chatMsg_0))
			{
				return true;
			}
			if (!this.chatMsg_0.IsCustomSmiley())
			{
				return false;
			}
			if (this.chatMsg_0.IsSender())
			{
				return false;
			}
			EmojiDownloadInfo emojiDownloadInfo = EmojiImageService.getEmojiDownloadInfo(this.chatMsg_0.strMsg);
			if (emojiDownloadInfo == null)
			{
				return false;
			}
			if (emojiDownloadInfo.nGameExtType > 0)
			{
				return false;
			}
			CustomSmiley customSmileyByMd = CustomSmileyManager.GetCustomSmileyByMd5(emojiDownloadInfo.strMd5);
			return customSmileyByMd == null;
		}
		private void addToFav_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0.IsCustomSmiley())
			{
				EmojiDownloadInfo emojiDownloadInfo = EmojiImageService.getEmojiDownloadInfo(this.chatMsg_0.strMsg);
				if (emojiDownloadInfo != null && !string.IsNullOrEmpty(emojiDownloadInfo.strMd5) && (!string.IsNullOrEmpty(this.chatMsg_0.strThumbnail) || !string.IsNullOrEmpty(this.chatMsg_0.strPath)))
				{
					CustomSmiley customSmiley = CustomSmileyManager.GetCustomSmileyByMd5(emojiDownloadInfo.strMd5);
					if (customSmiley != null)
					{
						return;
					}
					customSmiley = new CustomSmiley(CustomSmileyType.CustomAdd, this.chatMsg_0.strThumbnail, this.chatMsg_0.strPath, emojiDownloadInfo.strMd5);
					if (CustomSmileyManager.addCustomSmiley(customSmiley))
					{
						this.addToFav.set_Visibility(1);
						MessageBoxManager.ShowToast(strings.QQFriend_Added, 2500, null, false);
						ChatPageCtrl cuChatPage = ChatContainerPage.getCuChatPage();
						if (cuChatPage != null && cuChatPage.mInputer != null && cuChatPage.mInputer.SmileyView != null)
						{
							cuChatPage.mInputer.SmileyView.RefreshCustomSmileys();
							return;
						}
					}
				}
			}
			else
			{
				if (!this.chatMsg_0.IsTxt())
				{
					return;
				}
				string text = "";
				string text2 = "";
				if (!ChatMsgMgr.getMsgUser(this.chatMsg_0, ref text, ref text2))
				{
					Log.smethod_1("BaseChatContent", "getMsgUser error:" + text2);
					return;
				}
				EmojiManager.GetInstance().SaveEmojiToFav(text2);
				this.addToFav.set_Visibility(1);
				MessageBoxManager.ShowToast(strings.QQFriend_Added, 2500, null, false);
			}
		}
		private void copyItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			string text = "";
			string text2 = "";
			if (!this.chatMsg_0.IsTxt())
			{
				if (this.chatMsg_0.isQmessageTxt())
				{
					if (!this.chatMsg_0.IsSender())
					{
						if (!ChatMsgMgr.getQQOfflineMsg(this.chatMsg_0.strMsg, ref text, ref text2))
						{
							Log.smethod_1("BaseChatContent", "getQQMsgUser error:" + text2);
							return;
						}
					}
					else
					{
						text = this.chatMsg_0.strTalker;
						text2 = this.chatMsg_0.strMsg;
					}
					text2 = EmojiManager.GetInstance().getUtf16CodeString(text2);
					Clipboard.SetText(text2);
				}
				return;
			}
			if (!ChatMsgMgr.getMsgUser(this.chatMsg_0, ref text, ref text2))
			{
				Log.smethod_1("BaseChatContent", "getMsgUser error:" + text2);
				return;
			}
			text2 = EmojiManager.GetInstance().getUtf16CodeString(text2);
			Clipboard.SetText(text2);
		}
		private void forwardItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0.IsVideo())
			{
				DownloadVideoContext.ProgressInfo progress = ServiceCenter.sceneDownloadVideo.getProgress(this.chatMsg_0.nMsgSvrID);
				if (this.chatMsg_0.nStatus == 2 && progress == null)
				{
					if (this.mChatElement != null)
					{
						ChatVideoItem chatVideoItem = this.mChatElement as ChatVideoItem;
						if (chatVideoItem != null && chatVideoItem.mTrans != null)
						{
							ForwardMsgHelper.ToForwardMsg(this.chatMsg_0, ForwardKind.Kind_NormalMsg, chatVideoItem.mTrans.nDuration);
							return;
						}
					}
				}
				else
				{
					if (this.chatMsg_0 == null || this.mChatElement == null)
					{
						return;
					}
					if (!this.chatMsg_0.IsSender())
					{
						ToolFunction.GotoPageForUserControl("/Source/UI/Chat/VideoPlayerPage.xaml", new object[]
						{
							this.chatMsg_0,
							this.mChatElement
						}, false);
						return;
					}
				}
			}
			else
			{
				ForwardMsgHelper.ToForwardMsg(this.chatMsg_0, ForwardKind.Kind_NormalMsg, null);
			}
		}
		private void resendItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.resendTalkMsg(this.chatMsg_0, -1);
		}
		private void rereceiveItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.rereceiveTalkMsg(this.chatMsg_0);
		}
		private void delItem_Click(object sender, RoutedEventArgs e)
		{
			if (this.chatMsg_0 == null)
			{
				return;
			}
			if (this.chatMsg_0 is ChatTimeMsg)
			{
				return;
			}
			ChatMsgMgr.deleteTalkMsg(this.chatMsg_0);
		}
		public BaseChatContent()
		{
			this.InitializeComponent();
			this.init(true);
		}
		public BaseChatContent(bool createContextMenu = true)
		{
			this.InitializeComponent();
			this.init(createContextMenu);
		}
		public void init(bool createContextMenu)
		{
			if (createContextMenu)
			{
				this.CreatMenu();
			}
			base.SetValue(MMTiltEffect.IsTiltEnabledProperty, true);
			base.add_Loaded(new RoutedEventHandler(this.BaseChatContent_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.BaseChatContent_Unloaded));
		}
		private void BaseChatContent_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mIsLoaded = false;
			this.RefreshGlitterFlag();
		}
		private void BaseChatContent_Loaded(object sender, RoutedEventArgs e)
		{
			this.mIsLoaded = true;
			this.RefreshGlitterFlag();
		}
		public override void OnApplyTemplate()
		{
			base.OnApplyTemplate();
			this.Container = (base.GetTemplateChild("Container") as Border);
			this.CornerPath = (base.GetTemplateChild("Corner") as Path);
			this.RefreshContentType();
		}
		public void ClearBk()
		{
			this.mBIsTransparentContainer = true;
			this.BkBrush = null;
			this.CornerImg = null;
			if (this.Container != null)
			{
				this.Container.set_BorderThickness(new Thickness(0.0, 0.0, 0.0, 0.0));
			}
		}
		private void RefreshContentType()
		{
			if (this.glittersStoryboard != null)
			{
				return;
			}
			if (this.mBIsTransparentContainer)
			{
				this.ClearBk();
				return;
			}
			if (this.Container != null)
			{
				this.Container.set_BorderThickness(new Thickness(0.0, 0.0, 0.0, 1.0));
			}
			if (this.ContentType == "VOICE")
			{
				if (this is LeftChatContent)
				{
					this.BkBrush = BaseChatContent.LeftVoiceNormalBrush;
					this.CornerImg = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/leftVoiceCorner.png") as ImageSource);
					return;
				}
				if (this is RightChatContent)
				{
					this.BkBrush = BaseChatContent.RightVoiceNormalBrush;
					this.CornerImg = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/rightVoiceCorner.png") as ImageSource);
					return;
				}
				if (this is DriftVoiceContent)
				{
					this.BkBrush = BaseChatContent.DriftBottleVoiceNormalBrush;
					this.CornerImg = (new ImageSourceConverter().ConvertFromString("/Source/Plugin/DriftBottle/Resource/Img/VoiceIcon2.png") as ImageSource);
					return;
				}
			}
			else
			{
				if (this is LeftChatContent)
				{
					this.BkBrush = BaseChatContent.LeftTxtNormalBrush;
					this.CornerImg = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/leftTxtCorner.png") as ImageSource);
					return;
				}
				if (this is RightChatContent)
				{
					this.BkBrush = BaseChatContent.RightTxtNormalBrush;
					this.CornerImg = (new ImageSourceConverter().ConvertFromString("/Resource/Img/Chat/rightTxtCorner.png") as ImageSource);
				}
			}
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BaseChatContent.ContentTypeProperty)
			{
				(dependencyObject_0 as BaseChatContent).OnContentTypePropertyChanged(dependencyPropertyChangedEventArgs_0);
				return;
			}
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BaseChatContent.BkBrushProperty)
			{
				(dependencyObject_0 as BaseChatContent).OnBkBrushPropertyChanged(dependencyPropertyChangedEventArgs_0);
				return;
			}
			if (dependencyPropertyChangedEventArgs_0.get_Property() == BaseChatContent.CornerImgProperty)
			{
				(dependencyObject_0 as BaseChatContent).OnCornerImgPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnContentTypePropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshContentType();
		}
		private void OnBkBrushPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void OnCornerImgPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
		}
		private void StartGlitters()
		{
			if (this.glittersStoryboard != null)
			{
				return;
			}
			this.glittersStoryboard = this.mGlittersService.StartStoryboard();
			Binding binding = new Binding("ItemsOpacity");
			binding.set_Mode(3);
			binding.set_Source(this.mGlittersService);
			base.SetBinding(UIElement.OpacityProperty, binding);
			Log.smethod_5("BaseChatContent", "StartGlitters");
		}
		private void EndGlitters()
		{
			if (this.glittersStoryboard == null)
			{
				Binding binding = new Binding("Opacity");
				binding.set_Mode(3);
				binding.set_Source(this);
				base.SetBinding(UIElement.OpacityProperty, binding);
				base.set_Opacity(1.0);
				return;
			}
			Log.smethod_5("BaseChatContent", "EndGlitters");
			this.mGlittersService.EndStoryboard();
			Binding binding2 = new Binding("Opacity");
			binding2.set_Mode(3);
			binding2.set_Source(this);
			base.SetBinding(UIElement.OpacityProperty, binding2);
			base.set_Opacity(1.0);
			this.glittersStoryboard = null;
		}
		public void RefreshGlitterFlag()
		{
			if (this.mIsLoaded && this.mIsGlitterFlag)
			{
				this.ShouldGlitter = true;
				return;
			}
			this.ShouldGlitter = false;
		}
		private void DoGlitter()
		{
			if (this.mShouldGlitter)
			{
				this.StartGlitters();
				return;
			}
			this.EndGlitters();
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Chat/ChatListItem/BaseChatContent.xaml", 2));
		}
	}
}
